<?php
namespace app\admin\model;
use think\Model;
use think\Db;
use QRcode;
class shopModel extends Model{
    public $table = 'shop';
    public $shop_type ='shop_type';

    public function typeList(){
        $list = Db::name($this->shop_type)->where(['status'=>1])->order('sort DESC')->select();
        return $list;
    }

    /**
     * @param array $list
     * 获取type名称
     */
    public function getTypeName($list=''){
        $lists = self::typeList();
        foreach ($list AS $k=>$item){
            foreach ($lists as $key=>$value){
                if($item['type_id'] == $value['id']){
                    $list[$k]['typeName'] = $value['name'];
                }
            }
        }
        return $list;
    }
    public function getStatus(){
        return [0=>'审核',1=>'正常',2=>'锁定'];
    }
    public function getListTname($list = []){
        $status = $this->getStatus();
        foreach ($list as $key=>$value){
            foreach ($status as $k=>$v){
                if($value['status'] == $k){
                    $list[$key]['statusName'] = $v;
                }
            }
        }
        return $list;
    }

    /**
     * @param string $url
     * @param int $id
     * 商家二维码
     */
    public function rqCode($url='',$id=0){
        if(!file_exists("./static/theme/img/rqcode/shoprqcode{$id}.png")){
            require_once APP_PATH.'common/WxpayApi/example/phpqrcode/phpqrcode.php';
            $value = $id; //二维码内容
            $errorCorrectionLevel = 'L';//容错级别
            $matrixPointSize = 6;//生成图片大小
            $filerqcode = './static/theme/img/rqcode/shoprqcode'.$id.'.png';
            //生成二维码图片
            QRcode::png($value,$filerqcode, $errorCorrectionLevel, $matrixPointSize, 2);
            $logo = $url;//准备好的logo图片
            $QR = $filerqcode;//已经生成的原始二维码图
            if ($logo !== FALSE) {
                $QR = imagecreatefromstring(file_get_contents($QR));
                $logo = imagecreatefromstring(file_get_contents($logo));
                $QR_width = imagesx($QR);//二维码图片宽度
                $QR_height = imagesy($QR);//二维码图片高度
                $logo_width = imagesx($logo);//logo图片宽度
                $logo_height = imagesy($logo);//logo图片高度
                $logo_qr_width = $QR_width / 5;
                $scale = $logo_width/$logo_qr_width;
                $logo_qr_height = $logo_height/$scale;
                $from_width = ($QR_width - $logo_qr_width) / 2;
                //重新组合图片并调整大小
                imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
                    $logo_qr_height, $logo_width, $logo_height);
            }
            //输出图片
            imagepng($QR, $filerqcode);
        }
        return;
    }

    /**
     * 图片更新
     */
    public function imgUpdata(){

    }

    /**
     * 获取地区
     */
    public function address($info=[]){
       $area_id = Db::name('area')->where(['ID'=>$info['area_id']])->find();
        if($area_id['ParentId'] !==0){
            $ad = $this->address($area_id);
        }
            return $ad;
    }

    /**
     * @param string $newpass
     * @param string $id
     * @return bool
     * 密码修改
     */
    public function passwordEdit($newpass='',$id =''){
        $info = Db::name($this->table)->where(['id'=>$id])->find();
        if(($info['password'] != md5($newpass)) AND ($info['password'] != $newpass)){
            $resule = Db::name($this->table)->where(['id'=>$id])->update(['password'=>md5($newpass)]);
        }
        if(!empty($resule)){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 城市
     */
    public function Cities(){
        //一级地区
        $info['addlist'] = Db::name('area')->field('ID,ParentId,ShortName')->select();
        return $this->editkey($this->Tree($info['addlist']));
    }

    protected function Tree($list,$pid=100000){
        $result = [];
        foreach($list as $k=>$v){
            if($v['ParentId'] == $pid){
                unset($list[$k]);
                $v['children'] = $this->Tree($list,$v['ID']);
                if(!$v['children']){
                    unset($v['children']);
                }
                $result[] = $v;
            }
        }
        return $result;
    }

    public function editkey($list=''){
        $data = [];
        foreach ($list as $k=>$v){
            $data[$k]['id'] = $v['ID'];
            $data[$k]['name'] = $v['ShortName'];
            if(isset($v['children']) && $v['children']){
                foreach ($v['children'] as $key=>$value){
                    $data[$k]['children'][$key]['id'] = $value['ID'];
                    $data[$k]['children'][$key]['name'] = $value['ShortName'];
                    if(isset($value['children']) && $value['children']){
                        foreach ($value['children'] as $ke=>$va){
                            $data[$k]['children'][$key]['children'][$ke]['id'] = $va['ID'];
                            $data[$k]['children'][$key]['children'][$ke]['name'] = $va['ShortName'];
                        }
                    }
                }
            }
        }

        return $data;
     }
     //店铺类别
     public function shopclass($list=[]){
        if(!empty($list)){
            foreach ($list as $v){
                $cid[] = $v['cid'];
            }
            if(!empty($cid)){
                $cid = implode(',',$cid);
                $class = Db::query("SELECT * FROM `t_shop_class` WHERE `id` IN ($cid)");
                foreach ($list as $k=>$v){
                    foreach ($class as $va){
                        if($v['cid'] == $va['id']){
                            $list[$k]['ctype'] = $va['name'];
                        };
                    }
                }
                return $list;
            }
        }else{
            return $list;
        }
     }
}